package main

import (
	"encoding/json"
	"fmt"
	"time"
)

func main() {
	structToJsonStrDemo01()
	//mapToJsonStrDemo2()
	//serialSlice()
	//serializeFloat()
}

type Monitor struct {
	Name  string `json:"name"`
	Age   int    `json:"age"`
	Skill string `json:"skill"`
}

// 转换obj为json

// 序列化结构体对象
func structToJsonStrDemo01() {
	monitor := Monitor{"孙悟空", 500, "金箍棒"}
	jsonStr, _ := json.Marshal(monitor)
	finalJsonStr := string(jsonStr)
	fmt.Println("jsonStr: ", string(jsonStr))
	fmt.Printf("%T", finalJsonStr)
}

// 序列化map对象
func mapToJsonStrDemo2() {
	var obj map[string]interface{} = make(map[string]interface{})
	obj["name"] = "yoyo"
	obj["age"] = 20
	obj["gender"] = "male"
	obj["birthday"] = time.Now()
	serialer, _ := json.Marshal(obj)
	jsonStr := string(serialer)
	fmt.Println("jsonStr: ", jsonStr)
}

// 序列化切片
func serialSlice() {
	var slice []map[string]interface{}
	m1 := make(map[string]interface{})
	m1["name"] = "kiko"
	m1["age"] = 20
	m1["gender"] = "male"
	m1["birthday"] = time.Now()
	m1["address"] = [2]string{"夏威夷", "三亚"}
	slice = append(slice, m1)
	m2 := make(map[string]interface{})
	m2["name"] = "yoyo"
	m2["age"] = 21
	m2["gender"] = "woman"
	m2["birthday"] = time.Now()
	m2["address"] = [2]string{"青岛", "香格里拉"}
	slice = append(slice, m2)
	serialer, _ := json.Marshal(slice)
	jsonStr := string(serialer)
	fmt.Println("jsonStr: ", jsonStr)
}

// 序列化float类型
func serializeFloat() {
	var f float64 = 12.34
	marshal, _ := json.Marshal(f)
	data := string(marshal)
	fmt.Println("data: ", data)
}

// 解析json为obj
